package com.qinggeng.project.system.service.impl;

import java.sql.Time;
import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;
import com.qinggeng.common.enums.PageTurnTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.domain.server.Sys;
import com.qinggeng.project.api.dto.ActivityGiftDTO;
import com.qinggeng.project.api.dto.SysActivityDTO;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.*;
import com.qinggeng.project.system.service.ICostFreeService;
import com.qinggeng.project.system.service.IGoodsGiftService;
import com.qinggeng.project.system.service.IOrdersGiftService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.poi.ss.formula.functions.Now;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qinggeng.project.system.mapper.SysActivityMapper;
import com.qinggeng.project.system.service.ISysActivityService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 系统活动Service业务层处理
 *
 * @author smallfish
 * @date 2021-01-12
 */
@Slf4j
@Service
public class SysActivityServiceImpl implements ISysActivityService
{
    @Resource
    private SysActivityMapper sysActivityMapper;
    @Autowired
    private IOrdersGiftService ordersGiftService;
    @Autowired
    private IGoodsGiftService goodsGiftService;
    @Autowired
    private ICostFreeService costFreeService;

    /**
     * 查询系统活动
     *
     * @param id 系统活动ID
     * @return 系统活动
     */
    @Override
    public SysActivity selectSysActivityById(Long id)
    {
        return sysActivityMapper.selectSysActivityById(id);
    }

    /**
     * 查询系统活动列表
     *
     * @param sysActivity 系统活动
     * @return 系统活动
     */
    @Override
    public List<SysActivity> selectSysActivityList(SysActivity sysActivity)
    {
        return sysActivityMapper.selectSysActivityList(sysActivity);
    }

    public List<SysActivityDTO> listSysActivityDTO(){
        String currentTime= DateUtils.dateTimeNow("HH:mm:ss");
        List<SysActivityDTO> sysActivityDTOList=sysActivityMapper.listSysActivityDTO(currentTime);
        return sysActivityDTOList;
    }

    public List<Long[]> listSysActivityGiftId(){
        List<Long[]> giftIdList= Lists.newArrayList();
        String currentTime= DateUtils.dateTimeNow("HH:mm:ss");
        List<String> list=sysActivityMapper.listSysActivityGiftId(currentTime);
        for (String giftId:list
             ) {
            if(StringUtils.isEmpty(giftId)){
                continue;
            }
            Long[] ids= null;
            try{
                ids=(Long[]) ConvertUtils.convert(giftId.split(","),Long.class);
            }catch (Exception e){
                log.error("String转换Long数组错误",e.getMessage());
            }
            if(null==ids||ids.length<1){
                continue;
            }
            giftIdList.add(ids);
        }
        return giftIdList;
    }

    /**
     * 新增系统活动
     *
     * @param sysActivity 系统活动
     * @return 结果
     */
    @Override
    public int insertSysActivity(SysActivity sysActivity)
    {
        this.checkSysActivity(sysActivity);
        return sysActivityMapper.insertSysActivity(sysActivity);
    }

    /**
     * 修改系统活动
     *
     * @param sysActivity 系统活动
     * @return 结果
     */
    @Override
    public int updateSysActivity(SysActivity sysActivity)
    {
        this.checkSysActivity(sysActivity);
        return sysActivityMapper.updateSysActivity(sysActivity);
    }

    /**
     * 批量删除系统活动
     *
     * @param ids 需要删除的系统活动ID
     * @return 结果
     */
    @Override
    public int deleteSysActivityByIds(Long[] ids)
    {
        return sysActivityMapper.deleteSysActivityByIds(ids);
    }

    /**
     * 删除系统活动信息
     *
     * @param id 系统活动ID
     * @return 结果
     */
    @Override
    public int deleteSysActivityById(Long id)
    {
        return sysActivityMapper.deleteSysActivityById(id);
    }


    private SysActivity checkSysActivity(SysActivity sysActivity){
        if(null==sysActivity){
            throw new CustomException("sysActivity is null");
        }
        if(sysActivity.getIsReturn().equals(PageTurnTypeEnum.APP.getValue())
                && StringUtils.isEmpty(sysActivity.getCode())
        ){
            throw new CustomException("界面编码不能为空");
        }
        if(sysActivity.getIsReturn().equals(PageTurnTypeEnum.HTML.getValue())
                &&StringUtils.isEmpty(sysActivity.getUrl())){
            throw new CustomException("跳转链接不能为空");
        }
        if(sysActivity.getIsActivity()==2
                &&(sysActivity.getBeginTime()==null||sysActivity.getEndTime()==null)){
            throw new CustomException("开始和结束时间不能为空");
        }
        return sysActivity;
    }

    @Override
    public boolean insertOrdersGift(Long ordersId){
        CostFree costFree=costFreeService.selectCostFreeByUserId(ApiUtils.getUserId());
        if(costFree!=null){
            return true;
        }
        List<Long[]> giftIdList=this.listSysActivityGiftId();
        for (Long[] giftId:giftIdList
             ) {
            GoodsGift goodsGift=goodsGiftService.getGoodsGiftIdByRandom(giftId);
            if(null==goodsGift){
                continue;
            }
            ordersGiftService.insertOrdersGift(ordersId,goodsGift,"20");
            costFreeService.updateCostFreeRecord(ApiUtils.getUserId());
        }
        return true;

    }

    public List<ActivityGiftDTO> listActivityGiftDTO(){
        return sysActivityMapper.listActivityGiftDTO();
    }


}
